package com.firefly.service.query.cms;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.firefly.dao.dao.cms.po.TypePo;
import com.firefly.dao.dao.cms.service.TypeService;
import com.firefly.dao.assembler.cms.TypeAssembler;
import com.firefly.dao.vo.cms.TypeTreeVo;
import com.firefly.dao.vo.cms.TypeVo;
import com.firefly.util.utils.TreeNodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 分类
 * @author: lzg(萤火科技 ： 854378082 @ qq.com)
 * @date: 2022/12/13
 **/
@Service
public class TypeQuery {
    private final TypeService typesService;
    private final TypeAssembler typeAssembler;
    @Autowired
    public TypeQuery(TypeService typesService, TypeAssembler typeAssembler){
        this.typeAssembler = typeAssembler;
        this.typesService = typesService;
    }
    // 获取所有
    public List<Map> tree() {
        // 查出所有菜单来
        QueryWrapper<TypePo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort","id");
        List<TypePo> list = typesService.list(queryWrapper);
        // 类型转换
        List<TypeTreeVo> listTree = typeAssembler.getTreeList(list);
        //树形结构数据生成
        List<Map> result = TreeNodeUtils.toTree(0, listTree.stream().collect(Collectors.toList()));
        return result;
    }
    public TypeVo getDetailById(int id){
        TypePo entity = typesService.getById(id);
        return typeAssembler.getVo(entity);
    }
}
